import mongoClient from "../db/mongo/index.js";

const dbName = process.env.MONGO_DB_COLLECTION_NAME;
const db = mongoClient.db(dbName);
const tokenCollection = db.collection("gw_token");

// 根据token获取userId
export async function getUserIdByToken(token) {
  try {
    const tokenDocument = await tokenCollection.findOne({ _id: token });
    if (tokenDocument) {
      return tokenDocument.userId;
    } else {
      return null;
    }
  } catch (error) {
    console.log(error);
    throw error;
  }
}
